
#include "fileio.h"

#include <stdio.h>
#include <stdint.h>

int save_to_file(char *filename, void *head, uint32_t head_len, void *data, uint32_t len)
{
	FILE *pf = fopen(filename, "w+");

	if(NULL != pf)
	{
		if(NULL != head)
		{
			fwrite(head, head_len, 1, pf);
		}
		if(NULL != data)
		{
			fwrite(data, len, 1, pf);
		}

		fflush(pf);
		fclose(pf);
	}

	return 0;
}

int load_in_file(char *filename, void *buf, uint32_t n_size)
{
	int ret = 0;

	FILE *pf = fopen(filename, "r");

	if(NULL != pf)
	{
		if(1 == fread(buf, n_size, 1, pf))
		{
			ret = 0;
		}
		else
		{
			ret = -1;
		}

		fclose(pf);
	}

	return ret;
}